package cn.llyong.rpc.common.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @description: spring工具类
 * @author: lvyong
 * @date: 2020年06月30日 18:49
 */
@Component
public class SpringContextUtil implements ApplicationContextAware, DisposableBean {

    private static Logger logger = LoggerFactory.getLogger(SpringContextUtil.class);

    private static ApplicationContext applicationContext = null;

    public SpringContextUtil() {

    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) {
        logger.debug("注入 ApplicationContext 到 SpringContextUtil:" + applicationContext);

        if (SpringContextUtil.applicationContext != null) {
            logger.warn("SpringContextUtil 中的 ApplicationContext 被覆盖, 原有 ApplicationContext 为:" + SpringContextUtil.applicationContext);
        }
        SpringContextUtil.applicationContext = applicationContext;
    }

    /**
     * 取得存储在静态变量中的 ApplicationContext.
     */
    public static ApplicationContext getApplicationContext() {
        assertContextInjected();
        return applicationContext;
    }

    /**
     * 从静态变量 applicationContext 中取得 Bean, 自动转型为所赋值对象的类型.
     *
     * @param <T>
     * @param beanName
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String beanName) {
        assertContextInjected();
        return (T) applicationContext.getBean(beanName);
    }

    /**
     * 从静态变量 applicationContext 中取得指定类型的 Bean
     */
    public static <T> T getBean(Class<T> requiredType) {
        assertContextInjected();
        return applicationContext.getBean(requiredType);
    }

    /**
     * 从静态变量 applicationContext 中取得指定类型的 Bean，并按指定的类型强转
     */
    public static <T> T getBean(String name, Class<T> clazz) {
        assertContextInjected();
        return getApplicationContext().getBean(name, clazz);
    }

    public static boolean containsBean(String name) {
        assertContextInjected();
        return getApplicationContext().containsBean(name);
    }

    public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
        assertContextInjected();
        return getApplicationContext().isSingleton(name);
    }

    public static Class<?> getType(String name) throws NoSuchBeanDefinitionException {
        assertContextInjected();
        return getApplicationContext().getType(name);
    }

    public static String[] getAliases(String name) throws NoSuchBeanDefinitionException {
        assertContextInjected();
        return getApplicationContext().getAliases(name);
    }

    /**
     * 清除 SpringContextUtil 中的 ApplicationContext 为 Null.
     */
    public static void clear() {
        logger.debug("清除 SpringContextUtil 中的 ApplicationContext:" + applicationContext);
        applicationContext = null;
    }

    /**
     * 实现 DisposableBean 接口,在 Context 关闭时清理静态变量.
     */

    @Override
    public void destroy() throws Exception {
        SpringContextUtil.clear();
    }

    /**
     * 检查ApplicationContext不为空.
     */
    private static void assertContextInjected() {
        if (applicationContext == null) {
            throw new IllegalStateException("applicationContext 未注入,请SpringContextUtil");
        }
    }
}